package com.abl.manage.controller.sys;

import com.abl.core.common.cache.Cache;
import com.abl.core.common.util.StringKit;
import com.abl.core.domain.db.SysDict;
import com.abl.core.domain.project.PageSearch;
import com.abl.core.domain.project.SRS;
import com.abl.core.mybatis.mapper.SysDictMapper;
import com.abl.core.service.sys.SysDictService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static com.abl.core.domain.db.table.SysDictTableDef.SYS_DICT;

@Slf4j
@RestController
@RequestMapping("/manage/system/dict")
public class SysDictC {
    @Resource
    private SysDictService dictService;

    @Resource
    private Cache<String> cache;

    @Resource
    private SysDictMapper dictMapper;

    @PostMapping("/insert")
    public SRS<Boolean> insert(SysDict param) {
        dictService.insert(param);
        return SRS.bySuccess(true);
    }

    @PostMapping("/delete")
    public SRS<Boolean> delete(String ids) {
        dictService.delete(ids);
        return SRS.bySuccess(true);
    }

    @PostMapping("/update")
    public SRS<Boolean> update(SysDict param) {
        dictService.update(param);
        return SRS.bySuccess(true);
    }

    @PostMapping("/page")
    public SRS<Page<SysDict>> page(SysDict param, PageSearch pageSearch) {
        Page<SysDict> page = dictService.page(param, pageSearch);
        return SRS.bySuccess(page);
    }

    @PostMapping("/clearCache")
    public SRS<Boolean> clearCache() {
        dictService.clearCache();
        return SRS.bySuccess(true);
    }

    @PostMapping("/updateSetting")
    @Transactional(rollbackFor = Exception.class)
    public SRS<Boolean> updateSetting(@RequestBody Map<String, String> param) {
        Set<String> paramKey = param.keySet();
        for (String key : paramKey) {
            SysDict sysDict = new SysDict();
            sysDict.setDictKey(key);
            sysDict.setDictValue(param.get(key));
            QueryWrapper wrapper = QueryWrapper.create().where(SYS_DICT.DICT_KEY.eq(key));
            List<SysDict> sysDicts = dictMapper.selectListByQuery(wrapper);
            if (sysDicts.size() == 0) {
                dictMapper.insertSelective(sysDict);
            } else {
                dictMapper.updateByQuery(sysDict, wrapper);
            }
        }
        dictService.clearCache();
        return SRS.bySuccess(true);
    }

    @PostMapping("/selectSetting")
    public SRS<HashMap<String, String>> selectSetting(String keys) {
        ArrayList<String> keysList = StringKit.split(keys);
        HashMap<String, String> hashMap = new HashMap<>();
        for (String key : keysList) {
            String string = dictService.getString(key, "");
            hashMap.put(key, string);
        }
        return SRS.bySuccess(hashMap);
    }

    @PostMapping("/dictValue")
    public SRS<String> dictValue(String key) {
        String enumDict = dictService.getString(key);
        return SRS.bySuccess(enumDict);
    }

    @PostMapping("/allDictEnum")
    public SRS<List<SysDict>> dictValue(SysDict param) {
        List<SysDict> enumDictList = dictService.allDictEnum(param);
        return SRS.bySuccess(enumDictList);
    }

}
